<?php
namespace Modules\Backstage\Controllers;

class SystemParameterController extends \Phalcon\Mvc\Controller {
	public function beforeExecuteRoute(\Phalcon\Mvc\Dispatcher $dispatcher) {
		$this -> response -> setStatusCode(200, 'OK') -> sendHeaders() -> setContentType('application/json', 'UTF-8');

		$service = $this -> account::getServiceInstance() -> checkLogin($this -> request -> getQuery('token', 'string', null));
		if($service['code'] != 0) {
			$this -> response -> setJsonContent(['code'=>-1,'msg'=>$service['message'] ]) -> send();
			exit;
		}

		$this -> account::getAclInstance() -> check($this, $dispatcher, $service['model'] -> roles_id);
	}


	public function findAction() {
		$service = $this -> parameter::getServiceInstance() -> find([
			'conditions' => 'status = 0'
		]);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>-1,'msg'=>'数据不存在!']);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success', 'models'=>$service['models']]);
	}

	public function findFirstByKeyAction($key) {
		$service = $this -> parameter::getServiceInstance() -> findFirstByKey($key);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>-1,'msg'=>'数据不存在!']);
		}

		$model = $service['model'] -> toArray();
		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success', 'model' => $model]);
	}

	public function updateByKeyAction($key) {
		$service = $this -> parameter::getServiceInstance() -> findFirstByKey($key);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>-1,'msg'=>'数据不存在!']);
		}
		
		switch($service['model'] -> type) {
			case 1:
			case '1':
				$service['model'] -> value = $this -> request -> getPut('value', 'special_chars', 'Nothing.', false); 
				break;
			case 241:
			case '241':
				$service['model'] -> value = $this -> request -> getPut('value', 'int', 0, false); 
				break;
			case 157:
			case '157':
				$service['model'] -> value = json_encode($this -> request -> getPut('value')); 
				break;
		}

		if(!$service['model'] -> save()) {
			return $this -> response -> setJsonContent(['code'=>-1,'msg'=>'修改失败']);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'修改成功']);
	}

	public function findFirstByACCOUNT_BACKSTAGE_BANK_INFOAction() {
		$service = $this -> parameter::getServiceInstance() -> findFirstByKey('ACCOUNT_BACKSTAGE_BANK_INFO');
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>-1,'msg'=>'数据不存在!']);
		}

		$model = $service['model'] -> toArray();
		$model['value'] = json_decode($model['value'], true);
		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success', 'model' => $model]);
	}

	public function updateByACCOUNT_BACKSTAGE_BANK_INFOAction() {
		$put = $this -> request -> getPut();
		$data['value'] = json_encode($put);
		$service = $this -> parameter::getServiceInstance() -> update('ACCOUNT_BACKSTAGE_BANK_INFO', $data);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'修改成功']);
	}
}